#!/usr/bin/env python
# coding: utf-8
# author: Wesley

import logging

from django.http import JsonResponse

from lib.common import AbstractHandler
from services.datamodel.version.models import mm_Version

app_logger = logging.getLogger('version')


class BaseHandler(AbstractHandler):
    def __init__(self):
        AbstractHandler.__init__(self, app_logger)
        self.app_logger = app_logger

    def handle_check_upgrade(self, request):
        self.checkMandatoryParams(request, ["ostype", "currver"])
        self.is_int_params(request, ['app_type', "ostype"])
        ostype = request.param_dict["ostype"]
        currver = request.param_dict["currver"]
        app_type = request.param_dict.get('app_type', 1)

        version_info = mm_Version.get_app_version_by_os(ostype, app_type)
        if not version_info:
            resp = {
                "retcode": 0,
                "url": '',
                "version": '',
                "force": False
            }
        elif version_info["version"] > currver:
            resp = {
                "retcode": 0,
                "url": version_info["url"],
                "version": version_info["version"],
                "force": True if currver < version_info["force_upgrade_before_version"] else False,
                "desc": version_info["desc"]
            }
        else:
            resp = {
                "retcode": 0,
                "url": '',
                "version": '',
                "force": False,
            }

        return JsonResponse(resp)

